﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 期末复习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        //点击载入
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=DESKTOP-2IE02NT;Database=qimo;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            SqlCommand sqlCommand2 = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand2.Connection = sqlConnection;
            sqlCommand.CommandText = "Select * from tb_class;";
            sqlCommand2.CommandText = "Select * from tb_student where No=@No;";
            sqlCommand2.Parameters.AddWithValue("@No", "3200707021");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable classTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(classTable);
            this.comboBox1.DataSource = classTable;
            this.comboBox1.DisplayMember = "Name";
            this.comboBox1.ValueMember = "No";
            SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
            if (sqlDataReader.Read())
            {
                this.textBox1.Text = sqlDataReader["No"].ToString();
                this.textBox2.Text = sqlDataReader["Name"].ToString();
                this.radioButton1.Checked = (bool)sqlDataReader["Gender"];
                this.radioButton2.Checked = !(bool)sqlDataReader["Gender"];
                this.dateTimePicker1.Value = (DateTime)sqlDataReader["BirthDate"];
                this.comboBox1.SelectedValue = (int)sqlDataReader["ClassNo"];
                this.textBox3.Text = sqlDataReader["Specialty"].ToString();
            }
            sqlDataReader.Close();
        }
        //点击更新
        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
               "Server=(local);Database=qimo;Integrated Security = sspi"; 
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText =
                "UPDATE tb_Student" +
                 " SET Name=@Name,Gender=@Gender,BirthDate=@BirthDate,ClassNo=@ClassNo,Specialty=@Specialty" +
                 " WHERE No=@No;";
            sqlCommand.Parameters.AddWithValue("@Name", this.textBox2.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Gender", this.radioButton2.Checked);
            sqlCommand.Parameters.AddWithValue("@BirthDate", this.dateTimePicker1.Value);
            sqlCommand.Parameters.AddWithValue("@ClassNo", (int)this.comboBox1.SelectedValue);
            sqlCommand.Parameters.AddWithValue("@Specialty", this.textBox3.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@No", "3200707001");
            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            MessageBox.Show($"更新{rowAffected}行。");
        }
    }
}
